package com.thon.controller.rest;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.thon.dao.util.Page;
import com.thon.entity.user.Coupon;
import com.thon.service.user.CouponService;

/** 
 * 类说明 :显示优惠券的列表
 * @author jiang
 * @date 创建时间：2014年6月30日 下午2:50:06 
 */
@Controller
@RequestMapping(value = "/api/coupon")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class CouponRestController {
	
	@Autowired
	private CouponService couponService;
	
	//显示所有优惠券的列表
	@RequestMapping(value="/list/all")
	public ResponseEntity<HttpEntity> findCouponList(@RequestParam(value="p",defaultValue="1") int pageNo,
			@RequestParam(value="s",defaultValue="10") int pageSize){
		Page<Coupon> page = new Page<Coupon>();
		page = couponService.findCouponPages(page);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page.desc("createdDate");
		return new ResponseEntity(page, HttpStatus.OK);
	}
	//通过优惠券类型过滤
	@RequestMapping(value="/list/bytype")
	public ResponseEntity<HttpEntity> findCouponByType(@RequestParam(value="p",defaultValue="1") int pageNo,
			@RequestParam(value="s",defaultValue="10") int pageSize,
			@RequestParam(required=false) String type,
			@RequestParam(required=false) int id){
		Page<Coupon> page = new Page<Coupon>();
		page = couponService.findCouponByType(type, id, pageNo, pageSize);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		return new ResponseEntity(page, HttpStatus.OK);
	}
	//通过优惠券的状态过滤
	@RequestMapping(value="/list/bystatus")
	public ResponseEntity<HttpEntity> findCouponByStatus(@RequestParam(value="p",defaultValue="1") int pageNo,
			@RequestParam(value="s",defaultValue="10") int pageSize,
			@RequestParam(required=false) int id,
			@RequestParam(required=false) String status){
		Page<Coupon> page = couponService.findCouponByStatus(status, id, pageNo, pageSize);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		return new ResponseEntity(page, HttpStatus.OK);
	}
	//根据项目id过滤
	@RequestMapping(value="/list/byprojectid")
	public ResponseEntity<HttpEntity>findCouponByProjectId(@RequestParam(value="p",defaultValue="1") int pageNo,
			@RequestParam(value="s",defaultValue="10") int pageSize,
			@RequestParam(required=false) int id,
			@RequestParam(required=false) int projectId){
		Page<Coupon> page = new Page<Coupon>();
		page = couponService.findCouponByProjectId(page, id, projectId);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		return new ResponseEntity(page, HttpStatus.OK);
	}
	//根据创建时间过滤
	@RequestMapping(value="/list/bycreatedate")
	public ResponseEntity<HttpEntity>findCouponByCreatedDate(@RequestParam(value="p",defaultValue="1") int pageNo,
			@RequestParam(value="s",defaultValue="10") int pageSize,
			@RequestParam(required=false) int id,
			@RequestParam(required=false) String createDt){
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Coupon> page = new Page<Coupon>();
		page = couponService.findCouponByCreateDt(createDt, id, pageNo, pageSize);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		map.put("page", page);
		return new ResponseEntity(page, HttpStatus.OK);
	}
	
 }
